/**
 * 
 */
package com.hssoftvn.android.apps.tinycouplechat;

import java.util.ArrayList;
import java.util.Calendar;

import com.hssoftvn.android.apps.tinycouplechat.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author TinySon
 *
 */
public class ChatItemApdapter extends ArrayAdapter<ChatItem>
{
	Context mContext;
	ArrayList<ChatItem> mValues;
	
	int [] TEXT_STATUS = {
		R.string.txt_status_sending,
		R.string.txt_status_sent,
		R.string.txt_status_delivered,
		R.string.txt_status_seen			
	};
	

	public ChatItemApdapter(Context context, ArrayList<ChatItem> objects) {
		super(context, R.layout.layout_chat_item_sender, objects);
		//super(context, R.layout.layout_chat_item_receiver, objects);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.mValues = objects;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		
		final ChatItem itemData = mValues.get(position);		
		
		View itemView = null;
		if (itemData.isLeftDisplayed())
			itemView = (View ) inflater.inflate(R.layout.layout_chat_item_sender, parent, false);
		else
			itemView = (View ) inflater.inflate(R.layout.layout_chat_item_receiver, parent, false);

		//PIC
		ImageView imgPic = (ImageView)itemView.findViewById(R.id.imgAvatar);		
		imgPic.setImageDrawable(itemData.getDrawablePicture());
		imgPic.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//Toast.makeText(mContext, "PIC is ZOOMED IN@", Toast.LENGTH_LONG).show();
			}
		});
		
		//Text
		TextView tvName = (TextView)itemView.findViewById(R.id.tvChatItem);
		tvName.setText(itemData.getText());
		
		//Status
		TextView tvStatus = (TextView)itemView.findViewById(R.id.tvStatus);			
		TextView tvUsername = (TextView)itemView.findViewById(R.id.tvUsername);

		if (itemData.isLeftDisplayed()){
			tvStatus.setText( 
					TinyCoupleChat.getInstance().getString(TEXT_STATUS[itemData.getStatus()]) + " at " +itemData.mSentTime
					);
			tvUsername.setText( 
				TinyCoupleChat.getInstance().getMyInfo().getUsername());
		}
		else {
			Calendar c = Calendar.getInstance(); 
    		int h = c.get(Calendar.HOUR);
    		int m = c.get(Calendar.MINUTE);
			tvStatus.setText( " Received at " + h + ":" + m);

			tvUsername.setText( 
					TinyCoupleChat.getInstance().getCouplexInfo().getUsername());
		}

		return itemView;
	}	
}
